package com.amigo.online.provider.user.controller;

import javax.annotation.Resource;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.amigo.online.common.base.controller.BaseController;
import com.amigo.online.common.core.response.Response;
import com.amigo.online.common.core.response.StatusCode;
import com.amigo.online.common.core.security.IgnoreSecurity;
import com.amigo.online.common.param.dto.sms.SmsDto;
import com.amigo.online.common.util.CommonValidator;
import com.amigo.online.provider.user.exception.UserException;
import com.amigo.online.provider.user.service.VerificationCodeService;


/**
 * 验证码相关
 *
 * @author amigo
 */
@RestController
@RequestMapping(value = "/sms", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class SmsController extends BaseController {

    @Resource
    private VerificationCodeService verificationCodeService;

    @IgnoreSecurity
    @PostMapping(value = "/code")
    public ResponseEntity<Response> SmsCode(@RequestBody SmsDto dto) {
        if (!CommonValidator.validate(dto)) {
            throw new UserException(StatusCode.BAD_REQUEST);
        }
        return success(verificationCodeService.sendSmsCode(dto.getMobilephone(), dto.getCountryCode(), dto.getType()));
    }

    @GetMapping(value = "/updatepassword/code")
    public ResponseEntity<Response> sendSmsForUserUpdatePassword() {
        return success(verificationCodeService.sendSmsForUserUpdatePassword());
    }
}
